﻿package core.image.psd
{
    import __AS3__.vec.*;
    import flash.geom.*;
    import flash.utils.*;

    public class Layer extends Object
    {
        public var Clipping:Boolean = false;
        public var Opacity:int = 0;
        public var Name:String = "";
        public var BlendModeKey:String = "";
        public var Dimensions:Rectangle;
        public var SortedChannels:Dictionary;
        public var MaskData:Mask;
        public var BlendingRangesData:BlendingRanges;
        public var Channels:Vector.<Channel>;
        public var Adjustments:Vector.<AdjustmentInfo>;
        public var Flags:int = 0;

        public function Layer()
        {
            this.Dimensions = new Rectangle();
            this.SortedChannels = new Dictionary();
            this.MaskData = new Mask();
            this.BlendingRangesData = new BlendingRanges();
            this.Channels = new Vector.<Channel>;
            this.Adjustments = new Vector.<AdjustmentInfo>;
            return;
        }// end function

        public static function Load(param1:ByteArrayReader) : Layer
        {
            var adjustmentLayerEndPos:*;
            var reader:*;
            var _arg1:* = param1;
            adjustmentLayerEndPos = NaN;
            var channel:*;
            reader = _arg1;
            var layer:* = new Layer;
            layer.Dimensions.y = reader.ReadInt32();
            layer.Dimensions.x = reader.ReadInt32();
            layer.Dimensions.height = reader.ReadInt32() - layer.Dimensions.y;
            layer.Dimensions.width = reader.ReadInt32() - layer.Dimensions.x;
            var numberOfChannels:* = reader.ReadUInt16();
            var i:*;
            while (i < numberOfChannels)
            {
                
                channel = Channel.Load(reader);
                layer.Channels.push(channel);
                layer.SortedChannels[channel.Id] = channel;
                i = (i + 1);
            }
            var signature:* = reader.ReadChars(4);
            if (signature != "8BIM")
            {
                throw new Error("Layer Channelheader error!");
            }
            layer.BlendModeKey = reader.ReadChars(4);
            layer.Opacity = reader.ReadByte();
            layer.Clipping = Boolean(reader.ReadByte() > 0);
            layer.Flags = reader.ReadByte();
            reader.ReadByte();
            var extraDataSize:* = reader.ReadUInt32();
            var extraDataPosition:* = reader.Position;
            layer.MaskData = Mask.Load(reader);
            layer.BlendingRangesData = BlendingRanges.Load(reader);
            var namePosition:* = reader.Position;
            layer.Name = reader.ReadPascalString();
            adjustmentLayerEndPos = extraDataPosition + extraDataSize;
            do
            {
                
                try
                {
                    layer.Adjustments.push(AdjustmentInfo.Load(reader));
                }
                catch (e:Error)
                {
                    reader.Position = adjustmentLayerEndPos;
                }
            }while (reader.Position < adjustmentLayerEndPos)
            reader.Position = adjustmentLayerEndPos;
            return layer;
        }// end function

    }
}
